package it.unipd.dei.project.iandroid;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ImageView;

public class Gallery extends Activity {

	private ArrayList<Foto> album = null;
	private FotoAdapter adapter = null;
	ImageView iw;
	Cursor cur;
	DatabaseOpenHelper db;
	SQLiteDatabase base;
	private int selected;
	int rotation;
	Display display;
	ExifInterface exif;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);

	}

	public void onResume() {
		super.onResume();
		db = new DatabaseOpenHelper(this);
		base = db.getWritableDatabase();

		Intent intent = getIntent();
		final int idPartita = intent.getExtras().getInt("id");

		this.album = new ArrayList<Foto>(100);
		cur = base.query("Foto", null, "IdPartita=" + idPartita, null, null, null, "Id ASC");
		for (int i = 0; i < cur.getCount(); i++) {
			cur.moveToNext();
			Bitmap image_to_upload;
			BitmapFactory.Options bfOptions = new BitmapFactory.Options();

			bfOptions.inDither = false;
			bfOptions.inSampleSize = 8;
			Bitmap file = BitmapFactory.decodeFile(cur.getString(2), bfOptions);
				if(file == null){
					base.delete("Foto", "Id=" + cur.getInt(0), null);
					continue;
				}
			Matrix mat = new Matrix();
			ExifInterface exif = null;
			try {
				exif = new ExifInterface(cur.getString(2));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			switch (orientation){
			case ExifInterface.ORIENTATION_ROTATE_270:
				mat.postRotate(270);
				image_to_upload = Bitmap.createBitmap(file, 0, 0, file.getWidth(), file.getHeight(), mat, true);
				this.album.add(new Foto(cur.getInt(0), image_to_upload));
				break;
			
			case ExifInterface.ORIENTATION_ROTATE_180:
				mat.postRotate(180);
				image_to_upload = Bitmap.createBitmap(file, 0, 0, file.getWidth(), file.getHeight(), mat, true);
				this.album.add(new Foto(cur.getInt(0), image_to_upload));
				break;
				
			case ExifInterface.ORIENTATION_ROTATE_90:
				mat.postRotate(90);
				image_to_upload = Bitmap.createBitmap(file, 0, 0, file.getWidth(), file.getHeight(), mat, true);
				this.album.add(new Foto(cur.getInt(0), image_to_upload));
				break;
			case ExifInterface.ORIENTATION_NORMAL:
				mat.postRotate(0);
				image_to_upload = Bitmap.createBitmap(file, 0, 0, file.getWidth(), file.getHeight(), mat, true);
				this.album.add(new Foto(cur.getInt(0), image_to_upload));
				break;
			}
			
		}
		cur.close();
		base.close();
		db.close();
		GridView pic = (GridView) findViewById(R.id.listaFoto);
		this.adapter = new FotoAdapter(this, R.layout.foto, this.album);
		// set the adapter
		pic.setAdapter(this.adapter);
		
		pic.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				
					Intent intent = new Intent(getApplicationContext(), Picture.class);
					String pkg = getPackageName();
					intent.putExtra(pkg + "position", position);
					intent.putExtra("id", idPartita);
					startActivity(intent);
					Log.d("Gallery.onCreate", "con id = " + id + " e position = " + position);
				
			}
		});
		
		iw =(ImageView) findViewById(R.id.camera);
		iw.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent camera = new Intent(getApplicationContext(), CameraPreview.class);
				camera.putExtra("id", idPartita);
				camera.putExtra("orientation", getResources().getConfiguration().orientation);
				startActivity(camera);
				
			}
			
		});

		pic.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> listView, View itemView, int position, long itemId) {
				selected = (int) position;
				System.err.println("Hai selezionato la foto" + selected);
				return false;
			}
		});
		// serve per attivare il long click
		registerForContextMenu(pic);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		cur.close();
		base.close();
		db.close();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.context_foto_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int fId = album.get(selected).getId();
		switch (item.getItemId()) {
		case R.id.delete:
			if (fId == -1) {
			} else {
				db = new DatabaseOpenHelper(this);
				base = db.getWritableDatabase();
				base.delete("Foto", "Id =" + fId, null);
				cur.close();
				base.close();
				db.close();
				onResume();
			}
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

}
